/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerLogin;
import enun.Perfil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import model.Login;
import util.MensagemUtil;

/**
 *
 * @author Root
 */
public class ViewMenu extends javax.swing.JFrame {

    private boolean habilitar = false;
    private Login login;
    private final ControllerLogin controllerLogin;

    public ViewMenu() {
        login = new Login();
        controllerLogin = new ControllerLogin();
        initComponents();
        habilitarCampos(habilitar);
        menuLogin.setVisible(false);
        criarCamboPerfil();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        botaoSair = new javax.swing.JToggleButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        comboUsuario = new javax.swing.JComboBox();
        campoSenha = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnTrocarUsuario = new javax.swing.JButton();
        lblusuarioLogado = new javax.swing.JLabel();
        lblLogado = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuDentista = new javax.swing.JMenuItem();
        menuPaciente = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        MenuConsulta = new javax.swing.JMenuItem();
        menuMarcaConsulta = new javax.swing.JMenuItem();
        menuLogin = new javax.swing.JMenu();
        menuCadastrarUsuario = new javax.swing.JMenuItem();

        jMenuItem2.setText("jMenuItem2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(javax.swing.UIManager.getDefaults().getColor("FormattedTextField.selectionBackground"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N
        jLabel1.setText("Sempre Sorriso");

        botaoSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        botaoSair.setText("SAIR");
        botaoSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSairActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setText("Consultório Odontológico");

        btnLogin.setText("Entrar");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnTrocarUsuario.setText("Mudar Usuario");
        btnTrocarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrocarUsuarioActionPerformed(evt);
            }
        });

        lblusuarioLogado.setText("Usuario logado:");

        lblLogado.setText("-");

        jMenu1.setText("Cadastro");

        menuDentista.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, 0));
        menuDentista.setText("Dentista");
        menuDentista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDentistaActionPerformed(evt);
            }
        });
        jMenu1.add(menuDentista);

        menuPaciente.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, 0));
        menuPaciente.setText("Paciente");
        menuPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPacienteActionPerformed(evt);
            }
        });
        jMenu1.add(menuPaciente);

        jMenuBar1.add(jMenu1);

        jMenu2.setText(" Guia");

        MenuConsulta.setText("Consultar Guia");
        MenuConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuConsultaActionPerformed(evt);
            }
        });
        jMenu2.add(MenuConsulta);

        menuMarcaConsulta.setText("Marcar Guia");
        menuMarcaConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuMarcaConsultaActionPerformed(evt);
            }
        });
        jMenu2.add(menuMarcaConsulta);

        jMenuBar1.add(jMenu2);

        menuLogin.setText("Login");

        menuCadastrarUsuario.setText("Cadastrar Usuario");
        menuCadastrarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCadastrarUsuarioActionPerformed(evt);
            }
        });
        menuLogin.add(menuCadastrarUsuario);

        jMenuBar1.add(menuLogin);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botaoSair, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(142, 142, 142)
                                        .addComponent(jLabel2))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(103, 103, 103)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(244, 244, 244)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(btnLogin)
                                                .addGap(18, 18, 18)
                                                .addComponent(btnTrocarUsuario))
                                            .addComponent(comboUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(31, 31, 31))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lblusuarioLogado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addComponent(lblLogado)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblusuarioLogado)
                    .addComponent(lblLogado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jLabel2)
                .addGap(34, 34, 34)
                .addComponent(comboUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(btnTrocarUsuario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addComponent(botaoSair, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_botaoSairActionPerformed

    private void menuDentistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDentistaActionPerformed
        ViewDentista d = new ViewDentista(login);
        d.setVisible(true);
    }//GEN-LAST:event_menuDentistaActionPerformed

    private void menuPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPacienteActionPerformed
        ViewPaciente p = new ViewPaciente(login);
        p.setVisible(true);
    }//GEN-LAST:event_menuPacienteActionPerformed

    private void menuMarcaConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuMarcaConsultaActionPerformed
        ViewGuia g = new ViewGuia(login);
        g.setVisible(true);
    }//GEN-LAST:event_menuMarcaConsultaActionPerformed

    private void MenuConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuConsultaActionPerformed
        ViewConsultarConsultasGuia c = new ViewConsultarConsultasGuia(login);
        c.setVisible(true);
    }//GEN-LAST:event_MenuConsultaActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        if (!habilitar) {
            if (validarCampos()) {
                carregarDadosLogin();
                boolean isValido = controllerLogin.verificarLogin(login);
                if (isValido) {
                    habilitarCampos(true);
                    lblLogado.setText(login.getPerfil().getDescricao());
                    JOptionPane.showMessageDialog(null, "Usuario Conectado !!!");
                    limparCampos();
                } else {
                    JOptionPane.showMessageDialog(null, "Usuario ou senha invalido");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null, "Usuario ja esta logado");
        }

    }//GEN-LAST:event_btnLoginActionPerformed

    private void menuCadastrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCadastrarUsuarioActionPerformed
        ViewLogin l = new ViewLogin(login);
        l.setVisible(true);
    }//GEN-LAST:event_menuCadastrarUsuarioActionPerformed

    private void btnTrocarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrocarUsuarioActionPerformed
        if (habilitar) {
            int status = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja sair ?", "Confirmação", JOptionPane.YES_NO_OPTION);
            if (status == 0) {
                habilitarCampos(false);
                JOptionPane.showMessageDialog(null, "Log-out");
                lblLogado.setText("");
                habilitar = false;
            }
        } else {
            JOptionPane.showMessageDialog(null, "Você não esta conectado no sistema");
        }

    }//GEN-LAST:event_btnTrocarUsuarioActionPerformed

    private void criarCamboPerfil() {
        List<Perfil> listaPerfil = new ArrayList<>();
        listaPerfil = Arrays.asList(Perfil.values());
        comboUsuario.removeAllItems();
        comboUsuario.addItem("Selecione");
        for (Perfil perfil : listaPerfil) {
            comboUsuario.addItem(perfil);
        }
    }

    private void carregarDadosLogin() {
        login.setPerfil((Perfil) comboUsuario.getSelectedItem());
        login.setSenha(campoSenha.getText());
    }

    private boolean validarCampos() {
        boolean isValido = true;

        if (comboUsuario.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Perfil");
        }
        if (campoSenha.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("Senha");
        }

        return isValido;
    }
   

    private void habilitarCampos(boolean habilitar) {
        this.habilitar = habilitar;
        menuLogin.setVisible(habilitar && Perfil.SECRETARIA_ADM == login.getPerfil());
        MenuConsulta.setEnabled(habilitar && (Perfil.SECRETARIA_ADM == login.getPerfil() || Perfil.SECRETARIA_CONSULTA == login.getPerfil()));
        menuMarcaConsulta.setEnabled(habilitar && Perfil.SECRETARIA_ADM == login.getPerfil());
        menuDentista.setEnabled(habilitar);
        menuPaciente.setEnabled(habilitar);

    }

    private void limparCampos() {
        campoSenha.setText("");
        comboUsuario.setSelectedItem(null);
        criarCamboPerfil();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MenuConsulta;
    private javax.swing.JToggleButton botaoSair;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnTrocarUsuario;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JComboBox comboUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblLogado;
    private javax.swing.JLabel lblusuarioLogado;
    private javax.swing.JMenuItem menuCadastrarUsuario;
    private javax.swing.JMenuItem menuDentista;
    private javax.swing.JMenu menuLogin;
    private javax.swing.JMenuItem menuMarcaConsulta;
    private javax.swing.JMenuItem menuPaciente;
    // End of variables declaration//GEN-END:variables
}
